/**
 * Chaxi License System 启动器
 * 这个文件提供了便捷的方式来启动系统的不同组件
 */

const path = require('path');
const { exec } = require('child_process');
const fs = require('fs');
const { app, BrowserWindow, dialog } = require('electron');

// 检查是否在Electron环境中运行
const isElectron = process.versions.hasOwnProperty('electron');

// 如果不是在Electron中运行，提供命令行界面
if (!isElectron) {
  console.log('=== Chaxi License System ===');
  console.log('');
  console.log('请选择要运行的组件:');
  console.log('1. 启动远程服务器');
  console.log('2. 打开Web授权生成器');
  console.log('3. 打开Web授权验证器');
  console.log('4. 打开Electron授权生成器');
  console.log('5. 打开Electron授权验证器');
  console.log('6. 退出');
  console.log('');
  
  const readline = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
  });
  
  readline.question('请输入选项 (1-6): ', (option) => {
    switch(option.trim()) {
      case '1':
        console.log('启动远程服务器...');
        exec('cd remote-server && node server.js', (error, stdout, stderr) => {
          if (error) {
            console.error(`执行错误: ${error}`);
            return;
          }
          console.log(stdout);
          if (stderr) console.error(stderr);
        });
        break;
        
      case '2':
        console.log('打开Web授权生成器...');
        const adminPath = path.join(__dirname, 'admin', 'keygen.html');
        openInBrowser(adminPath);
        break;
        
      case '3':
        console.log('打开Web授权验证器...');
        const clientPath = path.join(__dirname, 'client', 'auth.html');
        openInBrowser(clientPath);
        break;
        
      case '4':
        console.log('打开Electron授权生成器...');
        exec('electron admin/electron-keygen.html', (error) => {
          if (error) {
            console.error(`执行错误: ${error}`);
          }
        });
        break;
        
      case '5':
        console.log('打开Electron授权验证器...');
        exec('electron client/electron-auth.html', (error) => {
          if (error) {
            console.error(`执行错误: ${error}`);
          }
        });
        break;
        
      case '6':
        console.log('退出');
        break;
        
      default:
        console.log('无效选项，请重新运行并选择1-6之间的数字');
    }
    
    readline.close();
  });
} else {
  // 在Electron中运行时的逻辑
  let mainWindow;

  function createWindow() {
    mainWindow = new BrowserWindow({
      width: 800,
      height: 600,
      webPreferences: {
        nodeIntegration: true,
        contextIsolation: false
      }
    });

    mainWindow.loadFile(path.join(__dirname, 'launcher.html'));
    mainWindow.on('closed', function() {
      mainWindow = null;
    });
  }

  app.on('ready', createWindow);

  app.on('window-all-closed', function() {
    if (process.platform !== 'darwin') app.quit();
  });

  app.on('activate', function() {
    if (mainWindow === null) createWindow();
  });
}

// 在默认浏览器中打开HTML文件
function openInBrowser(filePath) {
  const fileUrl = `file://${filePath}`;
  
  // 检查操作系统并使用相应的命令
  switch(process.platform) {
    case 'darwin':
      exec(`open "${fileUrl}"`);
      break;
    case 'win32':
      exec(`start "" "${fileUrl}"`);
      break;
    default:
      exec(`xdg-open "${fileUrl}"`);
  }
}
